<?php
defined('IN_XLP') or exit('Access Denied');

class wxgroupAction extends commonAction {

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
        //G('path_mod', 2);
    }


    /*
     * 群组列表
     */
    public function index() {
		if(count($_POST)) {
		   $objData['close']=$_POST['act'];
		   M('wxgroup')->update($objData, array('groupid' => $_POST['id']));
		   $this->JsonReturn('ok', null, 1);
		}else{
			$p = $this->_getid("p",1);
			$fieldid = $this->_getid('fieldid', 0);
			$q=$this->_get("q");
			$fieldlist=M('wxgroup_profield')->findAll();

				$countWhere=array();
				$where = 'where ';
				if($fieldid){
					$countWhere['fieldid']=$fieldid;
					$where.='a.fieldid ="'.$fieldid.'"';
				}
				
				if($q){
					$countWhere['LIKE'] = array('groupname' => $q);
					$where.='a.groupname like "%'.$q.'%"';
				}
				$where=$where!='where '?$where:"";
				$pageShow=20;
				$rs=M('wxgroup')->query("select a.*,b.title as vtitle from __TABLE__ as a left join `hz_village` as b on a.village_id=b.vid  ".$where." LIMIT " . ($p - 1) * $pageShow . ',' . $pageShow);
				$total=M('wxgroup')->where($countWhere)->count();

				$this->assign('p', $p);
				$this->assign('total', $total);
				$this->assign('fieldid', $fieldid);
				$this->assign('fieldlist', $fieldlist);
				foreach($fieldlist as $fk=>$fv){
					$fieldnames[$fv['fieldid']]=$fv['title'];
				}
				$this->assign('fieldnames', $fieldnames);
				$this->assign(array("rs"=>$rs,"p"=>$p,"total"=>$total));
				$this->display();
		}
    }


    /*
     * 添加群组
     */
    public function add() {

        if(count($_POST)) {
            //是否关注及加入社区
            $data['groupname'] = $this->_post('groupname');
			$data['fieldid'] = $this->_postid('fieldid');
            $result = M('wxgroup')->insert($data);
            if($result) {
                $this->JsonReturn('新建群组成功', null, 1);
            } else {
                $this->JsonReturn('新建群组失败', null, 0);
            }
        }else{
			$profieldlist=M('wxgroup_profield')->where(array("formtype"=>"text"))->findAll();
			$this->assign(array("profieldlist"=>$profieldlist));
		    $this->display();
		}
    }
    /*
     * 群组类型管理
	 */
    public function field() {
        if(count($_POST)){
		   $displayorder=$_POST['displayorder'];
		   foreach($displayorder as $dk=>$dv){
               $objData['displayorder']=$dv;
			   M('wxgroup_profield')->update($objData, array('fieldid' => $dk));
		   }
		   $this->JsonReturn('ok', null, 1);
		}else{
			$p=$this->_getid("p",1);
			$q=$this->_get("q");
			$formtype=$this->_get("formtype");
			$countWhere=array();
			$where = 'where ';
			if($formtype){
				$countWhere['formtype']=$formtype;
				$where.='formtype ="'.$formtype.'"';
			}
			
			if($q){
				$countWhere['LIKE'] = array('title' => $q);
				$where.='title like "%'.$q.'%"';
			}
			$where=$where!='where '?$where:"";
			$pageShow=10;
			$rs=M('wxgroup_profield')->query("select * from __TABLE__ ".$where." LIMIT " . ($p - 1) * $pageShow . ',' . $pageShow);
			$total=M('wxgroup_profield')->where($countWhere)->count();
			$this->assign(array("rs"=>$rs,"formtype"=>$formtype,"formtypelist"=>$this->_getFormtypeList(),"p"=>$p,"total"=>$total));
			$this->display();
		}
    }
    /*
     * 群组创建方式
	 */
    public function _getFormtypeList() {
		$formtypelist=array("text"=>"用户自输入创建","select"=>"用户单选加入","multi"=>"用户多选加入");
		return $formtypelist;
    }
    /*
     * 添加群组类型
	 */
    public function field_add() {
		if(isHave($_POST)){
		    $addDate['title']=$this->_post("title");
			if(M("wxgroup_profield")->where($addDate)->find()){
			    $this->JsonReturn('该类型名称已经存在');
			}
            $addDate['note']=$this->_post("note");
            $addDate['formtype']=$this->_post("formtype");
			if($addDate['formtype']=="select" || $addDate['formtype']=="multi"){
			    $addDate['choice']=$this->_post("choice",10);
			}
			if($addDate['formtype']!="select"){
			    $addDate['inputnum']=$this->_postid("inputnum",10);
			}
			$addDate['mtagminnum']=$this->_postid("mtagminnum",0);
			$addDate['manualmoderator']=$this->_postid("manualmoderator",0);
			$addDate['manualmember']=$this->_postid("manualmember",0);
			$addDate['displayorder']=$this->_postid("displayorder",0);
			$id = M('wxgroup_profield')->insert($addDate);
			if ($id) {
				$this->JsonReturn('ok', $id, 1);
			} else {
				$this->JsonReturn('数据插入失败');
			}
		}else{
            $defaultInfo=array("title"=>"","note"=>"","formtype"=>"text","inputnum"=>"10","choice"=>"","mtagminnum"=>"0","manualmoderator"=>"0","manualmember"=>"0","displayorder"=>"0");
			$this->assign('field_info', $defaultInfo);
		    $this->display();
		}
    }
    /*
     * 编辑商户
     */
    public function edit() {

        if(count($_POST)) {
            $uid  = $this->_postid('uid');
            $data = $this->_input();
            M('seller')->update($data, array('uid'=>$uid));
            $this->JsonReturn('编辑商户成功', null, 1);
        }

        $uid = $this->_getid('uid');
        $seller_info = M('seller')
            ->where(array('uid'=>$uid))
            ->find();

        $this->assign('seller_info', $seller_info);
        $this->display();
    }



}